<?php
//No direct access
defined('_JEXEC') or die('Restricted access');

jimport('joomla.application.component.view');

class OwnserviceViewBranch extends JView
{
	function display($tpl = null) {

		global $mainframe, $option;

		// Initialise variables
		$user		= & JFactory::getUser();
		$ownuser	= & Ownservice::getUser();
		$document	= & JFactory::getDocument();
		$params 	= & $mainframe->getParams('com_ownservice');

		// Make sure you are logged in and have the necessary access rights
		if($user->gid < 25) {
			if (!Ownservice::checkAccess( 'branch', 'manage' )) {
				JError::raiseWarning(500, JText::_('ALERTNOTAUTH') );
				$mainframe->redirect(JRoute::_('index.php?option=com_ownservice'));
			}
		}

		if( $this->getLayout() == 'form' ) {
			$this->_displayForm($tpl);
			return;
		} 

		//add style to document
		$document->addStyleSheet(JURI::base().'/components/com_ownservice/assets/css/style.css');
		$document->addStyleSheet(JURI::base().'/components/com_ownservice/assets/css/nav.css');

		//Get data form the model
		$model		= $this->getModel();
		$row		= $this->get('Data');
		//$profile	= $model->getUser($user->get('id'));

		// Create a user access object for the current user
		$access = new stdClass();
		$access->canView	= ($user->gid < 25)? Ownservice::checkAccess('branch','view','branch','all'):1;
		$access->canEdit	= ($user->gid < 25)? Ownservice::checkAccess('branch','edit','branch','all'):1;
		$access->canDelete	= ($user->gid < 25)? Ownservice::checkAccess('branch','delete','branch','all'):1;

		//set page title
		$params->set('page_title',	JText::_('OWNSERVICE_BRANCH_DETAIL'));
		$document->setTitle( $params->get('page_title') );

		$this->assignRef('user', $user);
		$this->assignRef('ownuser', $ownuser);
		$this->assignRef('row', $row);
		$this->assignRef('access', $access);
		//$this->assignRef('profile', $profile);

		parent::display($tpl);
	}

	function _displayForm() {
		global $mainframe, $option;

		// Initialise variables
		$user		= & JFactory::getUser();
		$gid		= $user->get('gid');
		$ownuser	= & Ownservice::getUser();
		$document	= & JFactory::getDocument();
		$editor		= &JFactory::getEditor();
		$params 	= & $mainframe->getParams('com_ownservice');

		//add style to document
		$document->addStyleSheet(JURI::base().'/components/com_ownservice/assets/css/style.css');
		$document->addStyleSheet(JURI::base().'/components/com_ownservice/assets/css/nav.css');

		//Get data form the model
		$model		= $this->getModel();
		$row		= &$this->get('Data');
		$countries	= &$this->get('Countries');
		$branchs	= &$this->get('Branchs');
		//$profile	= $model->getUser($user->get('id'));

		// check if it's an edit action
		if($row->id) {
			$canEdit	= ($gid<25)?Ownservice::checkAccess('branch', 'edit', 'branch', 'all'):1;
			if ( !$canEdit ) {
				JError::raiseWarning(500, JText::_('ALERTNOTAUTH') );
				$mainframe->redirect(JRoute::_('index.php?option=com_ownservice'));
			}
		} else {
			$canAdd		= ($gid<25)?Ownservice::checkAccess('branch', 'add', 'branch', 'all'):1;
			if ( !$canAdd) {
				JError::raiseWarning(500, JText::_('ALERTNOTAUTH') );
				$mainframe->redirect(JRoute::_('index.php?option=com_ownservice'));
			}
		}

		//set page title
		if($row->id) {
			$params->set('page_title',	JText::_('OWNSERVICE_EDIT_BRANCH'));
		} else {
			$params->set('page_title',	JText::_('OWNSERVICE_NEW_BRANCH'));
		}
		$document->setTitle( $params->get('page_title') );

		$lists = array();

		//build country select list
		$lists['country'] = OwnserviceHelper::buildcountryselect($countries, 'country',$row->country,1);

		//build branch select list
		$lists['branch'] = OwnserviceHelper::buildbranchesselect($branchs, 'agent_id', $row->agent_id, true, 'class="inputbox"');

		$this->assignRef('user', $user);
		$this->assignRef('ownuser', $ownuser);
		$this->assignRef('row', $row);
		$this->assignRef('lists', $lists);
		$this->assignRef('editor', $editor);

		parent::display($tpl);
	}
}
